package arm.dic.bean;

import arm.common.manager.TableManager;
import arm.db.tools.DBManager;
import arm.dic.entity.DicCategory;
import arm.dic.entity.DicMetricType;

import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.List;

public class DicCategoryBean
{

    private DicCategory dicCategory;
    private TableManager<DicCategory> tableManager;
    private DicMetricType selectedMetricType;

    public DicCategory getDicCategory() {
        return dicCategory;
    }

    public void setDicCategory(DicCategory dicCategory) {
        this.dicCategory = dicCategory;
    }

    public String viewTable ()
    {
        getTableManager().readFromDB();
        return "viewDicCategoryTable" ;
    }

    public TableManager<DicCategory> getTableManager () {
        if (tableManager==null)
        {
            tableManager=new TableManager<DicCategory>(DicCategory.class);
        }
        return tableManager;
    }

    public void setTableManager (TableManager<DicCategory> tableManager) {
        this.tableManager=tableManager;
    }

    public String create()
    {
        dicCategory = new DicCategory();
        return "editDicCategory";
    }

    public String save()
    {
        DBManager.saveOrUpdate(getDicCategory());
        return viewTable();
    }

    public String edit () {
        setDicCategory(getTableManager().getSelectedObj());
        return "editDicCategory";
    }

    public  String cancel () {
        return viewTable();
    }

    public void delete () {
        DBManager.delete(getTableManager().getSelectedObj());
        getTableManager().readFromDB();
    }

    public List<SelectItem> getMetricTypeList()
    {
        List<DicMetricType> objs = DBManager.readList(DicMetricType.class.getName());
        List<SelectItem> selectItems = new ArrayList();
        for (DicMetricType obj : objs)
        {
            selectItems.add(new SelectItem(obj, obj.getName()));
        }
        return selectItems;
    }
}